package hu.myai.triggers;

import hu.myai.api.tick.AbstractTickProvider;
import hu.myai.api.triggers.AbstractTrigger;
import hu.myai.indicators.pivot.LastLowIndicator;
import hu.myai.model.IndicatorVO;
import hu.myai.model.TriggerVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This trigger calculates if the values of two lows after each other are
 * increasing, meaning a increasing trend.
 *
 * @author bozbalint
 *
 * */
public class HigherLowTrigger extends AbstractTrigger {

    /**
     * The object used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * it contains the last arrived indicator
     * */
    private IndicatorVO lastIndicator;

    // Trigger is listen on last high indicator
    private String lastLowID;
    public void setLastLowIndicatorParameter(double parameter) {
        lastLowID = register(new LastLowIndicator(parameter));
    }


    public HigherLowTrigger(String name, AbstractTickProvider tick) {
        super(name, tick);
    }

    @Override
    protected TriggerVO triggerCalculation() {

        IndicatorVO recentIndicator = indicatorMap.get(lastLowID);

        // no value or the value is same as before, no aim to do check
        if (recentIndicator == null || !recentIndicator.isChanged())
            return null;

        if (lastIndicator != null && lastIndicator.getValue() < recentIndicator.getValue()) {

            TriggerVO triggerVO = new TriggerVO();
            triggerVO.setTradeType(-1);
            triggerVO.setRisk(5);

            lastIndicator = recentIndicator;

            log.debug("TriggerVO: " + triggerVO.toString());
            return triggerVO;
        }

        lastIndicator = recentIndicator;
        return null;
    }
}
